let axios = require('axios');
let fs = require('fs')
let Cookie = `DIS4=2adddd0d710843159f7d03ae2f324e74; _umdata=G9FEEDA4B69835CDF563350CC5A4A55FDEACE7A; Hm_lpvt_484788504bd0bc163a54b110d0dc003c=1547992299; Hm_lvt_484788504bd0bc163a54b110d0dc003c=1547897395,1547955832,1547958074,1547960356; ln=1; lu=59629549; MCU=f34d44d91f534653952d53a68c0cb3f8; qk_guid_appstore=4345dacc-8311-49d3-84ca-b62af4728e6f; lite_token=ab3ec286f002f40011c477ffbbd67bce; _uab_collina=154011689562372037419238; user_redirct_subtask_list=1`

// Cookie = `DIS4=9cfe73933af444a7b5abf1538b5b3200; ln=1; lu=59614630; _umdata=GB1CB58954AB1A947A717761293FDAD6A49D3B1; Hm_lpvt_484788504bd0bc163a54b110d0dc003c=1547989383; Hm_lvt_484788504bd0bc163a54b110d0dc003c=1547975923,1547976791,1547977210,1547979075; MCU=8b86b046641d46149d5175078aed430c; lite_token=231c404810d1d77c7b4830de26ec8c60; _uab_collina=154008718990809266595014; user_redirct_subtask_list=1`

function getTaskList() {
    return new Promise((resolve, reject) => {
        try {
            axios.get('https://qianka.com/s4/lite.subtask.list', {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Host': 'qianka.com',
                    'Accept': 'application/json, text/plain, */*',
                    Cookie,
                    'User-Agent': `Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16C101 version=2.0.2018091801 bid=com.kka.video`
                }
            }).then(res => {
                resolve(res.data);
            }).catch(err => {
                reject(err)
            })
        } catch (err) {
            reject(err)
        }
    })

}

function applyTask(id) {
    return new Promise((resolve, reject) => {
        try {
            axios.get(`https://qianka.com/s4/lite.subtask.start?task_id=${id}&quality=0`, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Host': 'qianka.com',
                    'Accept': 'application/json, text/plain, */*',
                    Cookie,
                    'User-Agent': `Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16C101 version=2.0.2018091801 bid=com.kka.video`
                }
            }).then(res => {
                resolve(res.data);
            }).catch(err => {
                reject(err)
            })
        } catch (err) {
            reject(err)
        }
    });
}
async function main() {
    try {
        let result = {};
        result = await getTaskList()
        let tasks = result.payload.tasks;
        let incoming = result.payload.incoming;
        fs.writeFileSync('./taskList.json', JSON.stringify(result))
        if (tasks && tasks.length && tasks[0].qty > 0) {
            let id = tasks[0].id;
            console.log(`检测到新任务${id}，自动申请中....`)
            result = await applyTask(id);
            console.log(result.payload.message)
        } else {
            console.log('未检测到新任务.')
        }
        console.log('---', new Date().getTime())
        setTimeout(async() => {
            await main();
        }, 4000)
    } catch (err) {
        console.log('错误了')
        console.log('---', new Date().getTime())
        console.log(err)
        setTimeout(async() => {
            await main();
        }, 4000)
    }
}

main();